﻿using Newtonsoft.Json;
using NLogManager;
using System;
using System.Collections.Generic;
using System.Text;
using WJ.DataSender.Service.Commen;
using WJ.DataSender.Service.Models;
using WJ.DataSender.Service.Models.RequestModel;

namespace WJ.DataSender.Service
{
    /// <summary>
    /// 公共处理逻辑
    /// </summary>
    public class CommenLogic
    {
        public static bool PrefectRequestContent(WjByBaseEquipRequestModel content)
        {
            var equips = CacheValues.EquipmentConfig.Cameras;
            if (equips.Count == 0)
            {
                LogHelper.Debug($"当前相机设备信息配置内容为空");
                return false;
            }
            content.AddressNo = equips[0].AddressNo;
            content.AddressDes = equips[0].AddressDes;
            content.Num = equips.Count;
            content.TimeStamp = CommenMethod.ConvertDateTimeToUtcs(DateTime.Now);
            var modelList = new List<EquipCommonInfo>();
            foreach (var item in equips)
            {
                var model = new EquipCommonInfo();
                model.EquipmentNo = item.EquipmentNo;
                model.EquipmentSN = item.EquipmentSN;
                model.EquipmentName = item.EquipmentName;
                model.EquipmentIP = item.EquipmentIP;
                model.EquipmentStatus = GetEquipStatus(model);
                model.Longitude = item.Longitude;
                model.Latitude = item.Latitude;
                modelList.Add(model);
            }
            content.ObjectList = modelList;

            //加签过程
            var jsonStr = JsonConvert.SerializeObject(content);
            Dictionary<string, string> kvList = new Dictionary<string, string>();
            content.Sign = CommenMethod.GenerateSign(jsonStr, new List<string> { "Sign" }, kvList);
            return true;
        }

        /// <summary>
        /// 完善上报所有感知设备信息的接口
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool PrefectRequestContent(WjByAllEquipRequestModel content)
        {
            var cameras = CacheValues.EquipmentConfig.Cameras;
            var lidars = CacheValues.EquipmentConfig.Lidars;
            var radars = CacheValues.EquipmentConfig.Radars;
            if (cameras.Count == 0 && lidars.Count == 0 && radars.Count ==0)
            {
                LogHelper.Debug($"当前配置文件中所有设备信息配置内容为空，无法上传整体设备信息！");
                return false;
            }
            content.AddressNo = cameras[0].AddressNo;
            content.AddressDes = cameras[0].AddressDes;
            content.TimeStamp = CommenMethod.ConvertDateTimeToUtcs(DateTime.Now);
            var modelList = new List<EquipCommonInfo>();
            var lidarList = new List<EquipCommonInfo>();
            var radarList = new List<EquipCommonInfo>();
            if (cameras != null && cameras.Count > 0)
            {
                foreach (var item in cameras)
                {
                    var model = new EquipCommonInfo();
                    model.EquipmentNo = item.EquipmentNo;
                    model.EquipmentSN = item.EquipmentSN;
                    model.EquipmentName = item.EquipmentName;
                    model.EquipmentIP = item.EquipmentIP;
                    model.EquipmentStatus = GetEquipStatus(model);
                    model.Longitude = item.Longitude;
                    model.Latitude = item.Latitude;
                    modelList.Add(model);
                }
                content.CameraList = modelList;
            }
            
            if(lidars != null && lidars.Count > 0)
            {
                foreach (var item in lidars)
                {
                    var model = new EquipCommonInfo();
                    model.EquipmentNo = item.EquipmentNo;
                    model.EquipmentSN = item.EquipmentSN;
                    model.EquipmentName = item.EquipmentName;
                    model.EquipmentIP = item.EquipmentIP;
                    model.EquipmentStatus = GetEquipStatus(model);
                    model.Longitude = item.Longitude;
                    model.Latitude = item.Latitude;
                    lidarList.Add(model);
                }
                content.LidarList = lidarList;
            }
            
            if(radars != null && radars.Count > 0)
            {
                foreach (var item in radars)
                {
                    var model = new EquipCommonInfo();
                    model.EquipmentNo = item.EquipmentNo;
                    model.EquipmentSN = item.EquipmentSN;
                    model.EquipmentName = item.EquipmentName;
                    model.EquipmentIP = item.EquipmentIP;
                    model.EquipmentStatus = GetEquipStatus(model);
                    model.Longitude = item.Longitude;
                    model.Latitude = item.Latitude;
                    radarList.Add(model);
                }
                content.RadarList = radarList;
            }
            //加签过程
            var jsonStr = JsonConvert.SerializeObject(content);
            Dictionary<string, string> kvList = new Dictionary<string, string>();
            content.Sign = CommenMethod.GenerateSign(jsonStr, new List<string> { "Sign" }, kvList);
            return true;
        }

        /// <summary>
        /// 获取设备状态值
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private static int GetEquipStatus(EquipCommonInfo model)
        {
            int status = 0;
            if (!CommenMethod.EquipmentIsOnline(model.EquipmentIP))
            {
                status = (int)Status.OFFLINE;
                return status;
            }
            return status;
        }
    }
}
